var select2;
var userForm;
$(function () {
    select2 = $('.select2').select2()
    var columns = [
        {
            field: 'ck',
            checkbox: true
        },
        {
            field: 'name',
            title: '分类名称'
        },
        {
            field: 'status',
            title: '分类状态',
            formatter: function (value, row, index) {
                if (value == 0) {
                    return '展示'
                }
                if (value == 1) {
                    return '收起'
                }
            }
        },
        {
            field: 'createDate',
            title: '创建时间',

        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row, index) {
                return '<button type="button" class="btn btn-primary  btn-xs" onclick="add(\'' + value + '\')"><i class="fa fa-edit"></i> 修改</button>' +
                    '<button type="button" class="btn btn-danger  btn-xs" onclick="deletes(\'' + value + '\')" style="margin-left: 10px;"><i class="fa fa-trash-o"></i> 删除</button>';
            }
        }
    ]
    createBootstrapTable($("#table"), '/sys/in/bookCategory/findPage', columns, true, 'toolbar', 'server')
})

var add_form;
var loading;

function add(val) {
    var title;
    if (val == undefined || val == null) {
        title = '新增信息'
    } else {
        title = '修改信息'
    }
    var url;
    if (val != undefined) {
        url = '/sys/in/bookCategory/form?id=' + val
    } else {
        url = '/sys/in/bookCategory/form'
    }
    add_form = layer.open({
        type: 2,
        title: title,
        shade: 0.6,
        area: ['500px', '400px'],
        content: url,
        offset: 'auto',
        btn: ['提交'],
        success: function (layero, index) {
            layer.iframeAuto(index);
        }, yes: function (layero, index) {
            loading = layer.load(2)
            $(index).find("iframe")[0].contentWindow.document.getElementById("btn").click()
        }
    })
}

function loaded(msg, code) {
    layer.close(loading)
    layer.msg(msg, {time: 1000}, function () {
        closeForm()
    })
}

// 关闭保存窗口
function closeForm() {
    layer.close(add_form)
    onLoads($("#href").val())
}

function deletes(id) {
    var ids = [];
    if (id != undefined) {
        ids.push(id)
    } else {
        $.each($('tbody input:checkbox:checked'), function () {
            ids.push($(this).parents('tr').data('uniqueid'));
        });
    }
    if (ids.length < 1) {
        layer.msg("最少请选择一条数据！")
        return;
    }
    var data = {
        ids: ids.toString()
    }

    layer.confirm('确定删除吗？', {
        title: '提示', btn: ['取消', '确定'] //按钮
    }, function () {
        layer.close(layer.index)
    }, function () {
        request('/sys/in/bookCategory/delete', 1, data, function (res) {
            if (res.code == 0) {
                layer.msg("删除成功", {time: 1000}, function () {
                    onLoads($("#href").val())
                })
            } else {
                layer.msg(res.msg, {time: 1000})
            }
        }, function (error) {
            layer.msg("系统异常！")
        })
    });
}

function resetForm() {
    $("#searchForm")[0].reset();
    $("#table").bootstrapTable('refresh');
}

// 查询
function searchForm(tableId, url, FormId) {
    var data = serializeObject("searchForm");
    var opt = {
        url: '/sys/in/bookCategory/findPage',
        silent: true,
        query: data
    };
    $("#table").bootstrapTable('refresh', opt);
}


